import { req } from '@/utils/BotRequest'

let session = null

// 服务器认证
export const verify = () => {
  return new Promise((resolve, reject) => {
    req.post('/verify', {
      verifyKey: '157617'
    }).then(res => {
      session = res.data.session
      resolve('验证成功')
    }).catch(err => {
      console.log(err)
      reject(err)
    })
  })
}

// 机器人绑定
export const bind = (qq) => {
  return new Promise((resolve, reject) => {
    if (!session) reject('请先认证')
    req.post('/bind', {
      sessionKey: session,
      qq: qq
    }).then(res => {
      // console.log(res)
      if (res.data.msg === 'success') resolve('绑定成功')
      reject('绑定失败')
    }).catch(err => {
      // console.log(err)
      reject(err)
    })
  })
}

// 机器人取消绑定 (释放)
export const release = () => {
  return new Promise((resolve, reject) => {
    if (!session) reject('请先认证')
    req.post('/release', {
      sessionKey: session,
      qq: 1559172136
    }).then(res => {
      if (res.data.msg === 'success') {
        session = null
        resolve('释放成功')
      }
      reject('释放失败')
    }).catch(err => {
      reject(err)
    })
  })
}

// 获取好友列表
export const getFriendList = () => {
  return new Promise((resolve, reject) => {
    req.get('/friendList?sessionKey=' + session).then(res => {
      resolve(res.data.data)
    }).catch(err => {
      reject(err)
    })
  })
}

// 删除好友
export const deleteFriend = (id) => {
  return new Promise((resolve, reject) => {
    if (!session) reject('请先认证')
    req.post('/deleteFriend', {
      sessionKey: session,
      target: id
    }).then(_res => {
      resolve()
    }).catch(() => {
      reject()
    })
  })
}

// 获取消息列表 -> 所有消息
export const countMessage = () => {
  return new Promise((resolve, reject) => {
    req.get('/peekMessage?sessionKey=' + session + '&count=' + 100).then(res => {
      // console.log(res.data.data)
      resolve(res.data.data)
    }).catch(err => {
      reject(err)
    })
  })
}

// 获取群聊列表
export const groupList = () => {
  return new Promise((resolve, reject) => {
    req.get('/groupList?sessionKey=' + session).then(res => {
      resolve(res.data.data)
    }).catch(err => {
      console.log(err)
      reject(err)
    })
  })
}

// 退出群聊
export const quit = (id) => {
  return new Promise((resolve, reject) => {
    req.post('/quit', {
      sessionKey: session,
      target: id
    }).then(res => {
      if (res.data.msg === 'success') resolve('退出成功')
      reject('退出失败')
    }).catch(err => {
      reject(err)
    })
  })
}

// 同意加入群聊
export const respBotInvitedJoinGroupRequestEvent = (item) => {
  return new Promise((resolve, reject) => {
    req.post('/resp/botInvitedJoinGroupRequestEvent', {
      sessionKey: session,
      eventId: item.eventId,
      fromId: item.fromId,
      groupId: item.groupId,
      operate: 0,
      message: ''
    }).then(res => {
      if (res.data.msg === 'success') resolve('成功加入群聊')
      reject('加入群聊失败')
    }).catch(err => {
      reject(err)
    })
  })
}

// 同意好友申请
export const respNewFriendRequestEvent = (item) => {
  return new Promise((resolve, reject) => {
    req.post('/resp/newFriendRequestEvent', {
      sessionKey: session,
      eventId: item.eventId,
      fromId: item.fromId,
      groupId: item.groupId,
      operate: 0,
      message: ''
    }).then(res => {
      if (res.data.msg === 'success') resolve('已同意')
      reject('同意失败')
    }).catch(err => {
      reject(err)
    })
  })
}

// 发送群消息
export const sendGroupMessage = (id, messageChain) => {
  return new Promise((resolve, reject) => {
    req.post('/sendGroupMessage', {
      sessionKey: session,
      target: id,
      messageChain
    }).then(res => {
      resolve(res.data.msg)
    }).catch(err => {
      reject(err)
    })
  })
}

export const fileUpload = (target, file) => {
  return new Promise((resolve, reject) => {
    const _1 = new FormData()
    _1.append('sessionKey', session)
    _1.append('type', 'group')
    _1.append('target', target)
    _1.append('file', file)
    _1.append('path', '1')
    _1.set('path', '')
    console.log(_1)
    req.post('/file/upload', _1, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }).then(res => {
      if (res.data.msg !== '') reject(res.data.msg)
      resolve()
    }).catch(err => {
      reject(err)
    })
  })
}
